/*
 * IndexBase
 *
 * Author: Lasse Collin <lasse.collin@tukaani.org>
 *
 * This file has been put into the public domain.
 * You can do whatever you want with this file.
 */

package org.tukaani.xz.index;

import org.tukaani.xz.XZIOException;
import org.tukaani.xz.common.Util;

abstract class IndexBase
{
  long blocksSum = 0;

  long uncompressedSum = 0;

  long indexListSize = 0;

  long recordCount = 0;


  IndexBase(XZIOException invalidIndexException)
  {
  }


  private long getUnpaddedIndexSize()
  {
    // Index Indicator + Number of Records + List of Records
    return 1 + Util.getVLISize(recordCount) + indexListSize;
  }


  public long getIndexSize()
  {
    return (getUnpaddedIndexSize() + 3) & ~3;
  }


  public long getStreamSize()
  {
    return Util.STREAM_HEADER_SIZE + blocksSum + getIndexSize() + Util.STREAM_FOOTER_SIZE;
  }


  int getIndexPaddingSize()
  {
    return (int) ((4 - getUnpaddedIndexSize()) & 3);
  }


  void add(long unpaddedSize, long uncompressedSize) throws XZIOException
  {
    blocksSum += (unpaddedSize + 3) & ~3;
    uncompressedSum += uncompressedSize;
    indexListSize += Util.getVLISize(unpaddedSize) + Util.getVLISize(uncompressedSize);
    ++recordCount;
  }
}
